<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of images
 *
 * @author Ron
 */
class images extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
//        $this->load->helper(array('form', 'url'));
//        $this->load->library('form_validation');
    }
    
    public function cut($imageName=NULL,$size=NULL){
        $size_arr = NULL;
        if(isset($imageName)){
            if(isset($size)){
                if(preg_match('/\A(\d+x\d+)|(\d+)\Z/', $size)!==0){
                    $size_arr = explode('x', $size);   
                }  else {
                    die('Url格式不正确');
                }
            }
            $this->cutImage('contents/images/'.$imageName, $size_arr[0], $size_arr[1]);
        }else{
            die('找不到该图片');
        }
    }
    public function cutImage($filePath,$newwidth=NULL,$newheight=NULL,$autoresize=FALSE){

        // Get new sizes
        $imgInfo = @getimagesize($filePath) or die('找不到该图片');
        $width = $imgInfo[0];
        $height = $imgInfo[1];
        $type = $imgInfo['mime'];
        
        $newheight=empty( $newheight)===TRUE?$height:$newheight;
        $newwidth=empty( $newwidth)===TRUE?$width:$newwidth;
        // Content type
        header('Content-type: '.$type);
        // Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        
        if($type=='image/jpeg')
        $source = imagecreatefromjpeg($filePath);
        if($type=='image/gif')
        $source = imagecreatefromgif($filePath);
        if($type=='image/png')
        $source = imagecreatefrompng($filePath);
        if($type=='image/vnd.wap.wbmp')
        $source = imagecreatefromwbmp($filePath);
//        switch ($type) {
//            case 'image/jpeg':
//                $source = imagecreatefromjpeg($filename);
//            case 'image/gif':
//                $source = imagecreatefromgif($filename);
//            case 'image/png':
//                $source = imagecreatefrompng($filename);
//            case 'image/vnd.wap.wbmp':
//                $source = imagecreatefromwbmp($filename);
//        }

        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        // Output
        $name = explode('.',basename($filePath));
        $newPath = dirname($filePath).'/'.$name[0].$newwidth.'x'.$newheight.'.'.$name[1];
        switch ($type) {
            case 'image/jpeg':
                imagejpeg($thumb);
                if(!file_exists($newPath));
                imagejpeg($thumb, $newPath);
                break;
            case 'image/gif':
                imagegif($thumb);
                if(!file_exists($newPath));
                imagegif($thumb, $newPath);
                break;
            case 'image/png':
                imagepng($thumb);
                imagepng($thumb, $newPath);
                break;
            case 'image/vnd.wap.wbmp':
               imagewbmp($thumb);
               imagewbmp($thumb, $newPath);
                break;
            default :
                die('不支持该图片格式');
        }
       imagedestroy($thumb);
       imagedestroy($source);
       //header('Location:'.'127.0.0.1:1080/MyLib/'.$newPath);
        
    }
}

?>
